﻿using LyData.Data;
using System;
using System.Data;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_SysLog : System.Web.UI.Page
{
    protected void Bind()
    {
        IDb db = new DbFactory().DriverDb();
        db.Open();
        DataTable table = db.DataTable("LogId,ManagerName,Url,Info,AddTime,AddIp", "log", "", "AddTime DESC");
        db.Close();
        this.LogList.DataSource = table;
        this.LogList.DataBind();
    }

    protected void LogList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.LogList.PageIndex = e.NewPageIndex;
        this.Bind();
    }

    protected void LogList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Button button = (Button)e.Row.FindControl("DelBt");
        if ((button != null) && !ManagerLogin.ValidateRole("C_SysLog.aspx_b"))
        {
            button.Enabled = false;
        }
    }

    protected void LogList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int rowIndex = e.RowIndex;
        string str = this.LogList.DataKeys[rowIndex].Values[0].ToString();
        IDb db = new DbFactory().DriverDb();
        db.Open();
        DateTime time = DateTime.Now.AddDays(-7.0);
        if (db.Scalar("count(LogId)", "log", string.Format("LogId={0} AND AddTime<'{1}'", str, time.ToString())) > 0)
        {
            db.Delete("log", string.Format("LogId={0}", str));
            this.Msg.Text = "日志删除成功！";
        }
        else
        {
            this.Msg.CssClass = "errorMsg";
            this.Msg.Text = "日志删除出错：只能删除 7 天前的日志！";
        }
        db.Close();
        this.Bind();
        this.Msg.Visible = true;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!ManagerLogin.ValidateRole("SysLog.aspx_a"))
        {
            this.Msg.CssClass = "errorMsg";
            this.Msg.Text = "您无此权限！";
            this.Msg.Visible = true;
        }
        else
        {
            this.Msg.CssClass = "msg";
            this.Msg.Visible = false;
            if (!base.IsPostBack)
            {
                this.Bind();
            }
        }
    }
}
